package cn.desk.user.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.desk.commons.entity.PageMode;
import cn.desk.commons.exception.DeskException;
import cn.desk.commons.service.impl.BaseServiceImpl;
import cn.desk.user.entity.User;
import cn.desk.user.service.IUserService;

public class UserServiceImpl<T extends User> extends BaseServiceImpl<T>
		implements IUserService<T> {
	public static final Logger logger = LoggerFactory
			.getLogger(UserServiceImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.desk.commons.service.impl.BaseServiceImpl#load(cn.desk.commons.entity
	 * .BaseEntity)
	 */
	public T load(T t) throws cn.desk.commons.exception.DeskException {
		logger.debug("load start");
		try {
			T temp = super.load(t);
			if (User.DELETECLASS_DELETED.equals(temp.getDeleteClass())) {
				throw new DeskException("");
			}
			return temp;
		} finally {
			logger.debug("load end");
		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.desk.commons.service.impl.BaseServiceImpl#listPage(cn.desk.commons
	 * .entity.PageMode, cn.desk.commons.entity.BaseEntity)
	 */
	public PageMode<T> listPage(PageMode<T> page, T t) {
		logger.debug("listPage start");
		try {
			t.setDeleteClass(User.DELETECLASS_NORMAL);
			return super.listPage(page, t);
		} finally {
			logger.debug("listPage end");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.desk.commons.service.impl.BaseServiceImpl#add(cn.desk.commons.entity
	 * .BaseEntity)
	 */
	public void add(T t) throws DeskException {
		logger.debug("add start");
		t.setDeleteClass(User.DELETECLASS_NORMAL);
		super.add(t);
		logger.debug("add end");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.desk.commons.service.impl.BaseServiceImpl#update(cn.desk.commons.entity
	 * .BaseEntity)
	 */
	public void update(T t) throws DeskException {
		logger.debug("update start");
		T temp = baseDao.load(t.getClass(), t.getId());
		if (temp == null) {
			throw new DeskException("EM0002", t);
		}
		temp.setName(t.getName());
		temp.setPassword(t.getPassword());
		temp.setDescription(t.getDescription());
		baseDao.modify(temp);
		logger.debug("update end");
	}
}
